package com.cn.wkditui;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;



/**
 * Created by wyd on 2016/3/15.
 */
public class WelComeActivity extends  BaseActivity  {

    private Button btjr;
    private String staffId="";
    private EditText et;
    private ListView lv;
    private DBHelper manager;
    private List<String> beans;
    private CheckBox ck;
    private LvAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        init();
   adapter =     new LvAdapter();
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                login(beans.get(i));
                ck.setChecked(false);
                lv.setVisibility(View.GONE);
            }
        });
        
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                Utils.ShowDialog(WelComeActivity.this, "是否删除该账户", new Utils.CallBack() {
                    @Override
                    public void onclickConfirm(Dialog dialog) {
                        dialog.dismiss();
                        deltele(beans.get(position));

                    }

                    @Override
                    public void onclickCancel(Dialog dialog) {
                        dialog.dismiss();
                    }
                });
                return true;
            }
        });
        

        ck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    lv.setVisibility(View.VISIBLE);
                } else {
                    lv.setVisibility(View.GONE);
                }
            }
        });
    }

    private void deltele(String s) {
        int a = manager.delete(s);
        beans = manager.getAll();
        if(a==1){

            adapter.notifyDataSetChanged();
        }
    }

    private void login(final String phone) {
        RequestParams params = new RequestParams();
        params.addBodyParameter("phone", phone);

        new HttpUtils().send(HttpRequest.HttpMethod.POST, Consts.HOST+Consts.CHECK, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
            Log.e("main","res"+responseInfo.result);
                try {
                    JSONObject json = new JSONObject(responseInfo.result);
                    int code = json.getInt("code");
                    if (code == 1) {
                        Intent intent = new Intent(WelComeActivity.this, MainActivity.class);
                        intent.putExtra("staffId", phone);
                        MyPreference.getInstance(WelComeActivity.this).setStaffid(phone);
                        startActivity(intent);
                    } else {
                        Toast.makeText(WelComeActivity.this, "账户不存在", 0).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(WelComeActivity.this, "请检查网络设置", 0).show();
                Log.e("...",s);
            }
        });

    }

    private void init() {
        et= (EditText) findViewById(R.id.et);
         manager = new DBHelper(this);
        ck = (CheckBox) findViewById(R.id.ck);
        manager.openDatabase();;
        beans= manager.getAll();
        lv = (ListView) findViewById(R.id.lv);
        btjr = (Button) findViewById(R.id.btjr);
        btjr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                manager.insert(et.getText().toString());
                if(TextUtils.isEmpty(et.getText().toString())){
                    Toast.makeText(WelComeActivity.this,"手机号不能为空",0).show();
                }else{
                    if(et.getText().toString().length()!=11){
                        Toast.makeText(WelComeActivity.this,"请输入正确的手机号",0).show();
                    }else{
//                        Intent intent = new Intent(WelComeActivity.this,MainActivity.class);
//                        MyPreference.getInstance(WelComeActivity.this).setStaffid(et.getText().toString());
//                        Log.e("",et.getText().toString());
//                        intent.putExtra("staffId",et.getText().toString());
//                        startActivity(intent );
                        login(et.getText().toString());
                    }
                }

            }
        });
    }


    class LvAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return beans.size();
        }

        @Override
        public Object getItem(int i) {
            return beans.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = View.inflate(WelComeActivity.this,R.layout.lv_wel,null);
            TextView tv = (TextView) view.findViewById(R.id.tv);
            tv.setText(beans.get(i));
            return view;
        }
    }
}
